package com.maoye.ossp.biz.intent.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.base.core.util.string.StringUtil;
import com.maoye.ossp.biz.contract.utils.ContractUtils;
import com.maoye.ossp.biz.intent.entity.IntAssociated;
import com.maoye.ossp.biz.intent.entity.IntSalesno;
import com.maoye.ossp.biz.intent.service.IIntAssociatedService;
import com.maoye.ossp.biz.intent.service.IIntSalesnoService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.sc.api.domain.User;
import com.maoye.sc.api.local.UserService;

/**
 * 对象功能:先申号审批表  控制器类
 * 开发公司:MOI
 * 开发人员:zhongjt
 * 创建时间:2014-02-20 14:50:38
 */
@Controller
@RequestMapping("biz/intent/intSalesno")
public class IntSalesnoController extends BaseController {
	@Resource
	private IIntSalesnoService intSalesnoService;
	
	@Resource
	private IIntAssociatedService intAssociatedService;
	
	@Resource UserService<User> userService;
	
	@RequestMapping(value = "list")
	public String list(IntSalesno intSalesno, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String contractType=request.getParameter("contractType");
		String createById =intSalesno.getCreateBy();
		String createBy = null;
		if(StringUtil.isNotEmpty(createById)){
			createBy = createById.split("\\|")[0];
		}
		QueryFilter queryFilter = getQuerFilter(request,intSalesno);
		queryFilter.addFilter("areaId", intSalesno.getAreaId(), QueryOP.EQUAL);
		queryFilter.addFilter("floorId", intSalesno.getFloorId(), QueryOP.EQUAL);
		queryFilter.addFilter("storeId", intSalesno.getStoreId(), QueryOP.EQUAL);
		if(StringUtil.isNotEmpty(intSalesno.getIntentNo())){
			queryFilter.addFilter("intentNo", intSalesno.getIntentNo(), QueryOP.LIKE);
		}
		
		queryFilter.addFilter("createBy", createBy, QueryOP.EQUAL);
		if(StringUtil.isNotEmpty(contractType)){
			queryFilter.addFilter("contractType", contractType, QueryOP.EQUAL);
		}
		
		Page<IntSalesno> page = (Page<IntSalesno>) intSalesnoService.find(queryFilter);
		model.addAttribute("contractType", contractType);
		model.addAttribute("page", page);
		model.addAttribute("intSalesno", intSalesno);
		
		return resultPage("/biz/intent/intSalesno/list", model, request);
	}

	@RequestMapping(value = "save")
	public String save(IntSalesno intSalesno, Model model,RedirectAttributes redirectAttributes, HttpServletRequest request,HttpServletResponse response) {
		String navId = request.getParameter("navId");
		String contractType =request.getParameter("contractType");
		intSalesno.setStatus(ContractUtils.INTENT_STATUS_BC);
		if(StringUtil.isNotEmpty(intSalesno.getCreateBy())){
			intSalesno.setCreateBy(intSalesno.getCreateBy().split("\\|")[0]);
		}
		if(StringUtil.isNotEmpty(intSalesno.getPurchaseBy())){
			intSalesno.setPurchaseBy(intSalesno.getPurchaseBy().split("\\|")[0]);
		}
		if(StringUtil.isNotEmpty(intSalesno.getGuarantor())){
			intSalesno.setGuarantor(intSalesno.getGuarantor().split("\\|")[0]);
		}
		intSalesnoService.save(intSalesno);
		addMessage(redirectAttributes, "保存'" + intSalesno.getIntentNo()+ "先申号流程'成功");
		return "redirect:/biz/intent/intSalesno/list?navId="+navId+"&contractType="+contractType;
	}

	/**
	 * 提交
	* @Title: submit
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param intSalesno
	* @param @param model
	* @param @param redirectAttributes
	* @param @param request
	* @param @param response
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "submit")
	public String submit(IntSalesno intSalesno, Model model,RedirectAttributes redirectAttributes, HttpServletRequest request,HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intSalesno.setStatus(ContractUtils.INTENT_STATUS_DS);
		String contractType =request.getParameter("contractType");
		if(StringUtil.isNotEmpty(intSalesno.getCreateBy())){
			intSalesno.setCreateBy(intSalesno.getCreateBy().split("\\|")[0]);
		}
		if(StringUtil.isNotEmpty(intSalesno.getPurchaseBy())){
			intSalesno.setPurchaseBy(intSalesno.getPurchaseBy().split("\\|")[0]);
		}
		if(StringUtil.isNotEmpty(intSalesno.getGuarantor())){
			intSalesno.setGuarantor(intSalesno.getGuarantor().split("\\|")[0]);
		}
		intSalesnoService.submit(intSalesno);
		addMessage(redirectAttributes, "提交'" + intSalesno.getIntentNo()+ "先申号流程'成功");
		return "redirect:/biz/intent/intSalesno/list?navId="+navId+"&contractType="+contractType;
	}
	
	
	
	@RequestMapping(value = "{intAssocoatedid}/add")
	public String add(@PathVariable Long intAssocoatedid,IntSalesno intSalesno, HttpServletRequest request,Model model) {
		//intSalesnoService.init(intSalesno);
		IntAssociated intAssciated =  intAssociatedService.get(intAssocoatedid);
		model.addAttribute("intentId", intAssciated.getId().toString());
		intAssciated.setCreateTime(new Date());
		intSalesno.setIntentId(intAssciated.getId().toString());
		intSalesno.setIntentNo(intAssciated.getIntentNo());
		intSalesno.setArea(intAssciated.getArea());
		intSalesno.setAreaId(intAssciated.getAreaId().toString());
		intSalesno.setStore(intAssciated.getStore());
		intSalesno.setStoreId(intAssciated.getStoreId().toString());
		//intSalesno.setShopId(intAssciated.gets);
		intSalesno.setSupplierId(intAssciated.getSupplierId());
		intSalesno.setSupplierName(intAssciated.getSupplierName());
		intSalesno.setSupplierNo(intAssciated.getSupplierNo());
		intSalesno.setFloor(intAssciated.getFloor());
		intSalesno.setFloorId(intAssciated.getFloorId());
		intSalesno.setCreateTime(new Date());
		intSalesno.setCreateBy(UserUtils.getCurrentUserId().toString());
		intSalesno.setStatus(ContractUtils.INTENT_STATUS_BC);
		model.addAttribute("intSalesno", intSalesno);
		User createByUser =userService.getUserById(UserUtils.getCurrentUserId().toString());
		model.addAttribute("createByName", createByUser.getName());
		model.addAttribute("contractType", intAssciated.getContractType());
		return resultPage("/biz/intent/intSalesno/add", model, request);
	}

	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable Long id, Model model,HttpServletRequest request) {
		IntSalesno intSalesno = intSalesnoService.get(id);
		if(StringUtil.isNotEmpty(intSalesno.getPurchaseBy())){
			User purchaseUser=userService.getUserById(intSalesno.getPurchaseBy());
			model.addAttribute("purchaseByName", purchaseUser.getName());
		}
		User createByUser =userService.getUserById(intSalesno.getCreateBy());
		if(StringUtil.isNotEmpty(intSalesno.getGuarantor())){
			User  guarantorUser =userService.getUserById(intSalesno.getGuarantor());
			model.addAttribute("guarantorByName", guarantorUser.getName());
		}
		User  updateUser =userService.getUserById(UserUtils.getCurrentUserId().toString());
		model.addAttribute("updateByName", updateUser.getName());
		
		intSalesno.setUpdateBy(UserUtils.getCurrentUserId().toString());
		model.addAttribute("createByName", createByUser.getName());
		model.addAttribute("contractType", intSalesno.getIntAssociated().getContractType());
		model.addAttribute("intSalesno", intSalesno);
		return resultPage("/biz/intent/intSalesno/add", model, request);
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable Long id, Model model,HttpServletRequest request,HttpServletResponse response) {
		IntSalesno intSalesno = intSalesnoService.get(id);
		model.addAttribute("intSalesno", intSalesno);
		if(StringUtil.isNotEmpty(intSalesno.getPurchaseBy())){
			User purchaseUser=userService.getUserById(intSalesno.getPurchaseBy());
			model.addAttribute("purchaseByName", purchaseUser.getName());
		}
		User createByUser =userService.getUserById(intSalesno.getCreateBy());
		if(StringUtil.isNotEmpty(intSalesno.getGuarantor())){
			User  guarantorUser =userService.getUserById(intSalesno.getGuarantor());
			model.addAttribute("guarantorByName", guarantorUser.getName());
		}
		if(StringUtil.isNotEmpty(intSalesno.getUpdateBy())){
			User  updaterUser =userService.getUserById(intSalesno.getUpdateBy());
			model.addAttribute("updateByName", updaterUser.getName());
		}
		String contractType = intSalesno.getIntAssociated().getContractType();
		String contract_Type = "";
		if(ContractUtils.CONTRACT_TYPE_LY.equals(contractType)){
			contract_Type ="intAssociated";
		}else if(ContractUtils.CONTRACT_TYPE_ZY_JX.equals(contractType) || ContractUtils.CONTRACT_TYPE_ZY_DX.equals(contractType)){
			contract_Type ="intSelfociated";
		}else if(ContractUtils.CONTRACT_TYPE_ZP.equals(contractType)){
			contract_Type ="intLeaseociated";
		}else if(ContractUtils.CONTRACT_TYPE_GG.equals(contractType)){
			contract_Type ="intAdvertociated";
		}else if(ContractUtils.CONTRACT_TYPE_CX.equals(contractType)){
			contract_Type ="intMarketociated";
		}
		model.addAttribute("contractType",contractType);
		model.addAttribute("contract_Type",contract_Type);
		model.addAttribute("createByName", createByUser.getName());
		return resultPage("/biz/intent/intSalesno/view", model, request);
	}

	@RequestMapping(value = "{ids}/delete")
	public String delete(@PathVariable String ids, Model model,RedirectAttributes redirectAttributes,HttpServletRequest request,HttpServletResponse response) {
		
		String navId = request.getParameter("navId");
		String contractType = request.getParameter("contractType");
		String[] idArr=ids.split(",");
		for (int i = 0; i < idArr.length; i++) {
			intSalesnoService.remove(Long.parseLong(idArr[i]));
		}
	
		addMessage(redirectAttributes, "删除先申号流程成功！");
		return "redirect:/biz/intent/intSalesno/list?navId="+navId+"&contractType="+contractType;
	}
}
